package utils

import (
	"math"
	"net/http"

	"github.com/labstack/echo/v4"
)

type returnJSON struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func JSONSuccess(c echo.Context, data interface{}, msg string) error {
	if msg == "" {
		msg = "success"
	}
	return c.JSON(http.StatusOK, &returnJSON{
		Code:	200,
		Msg:	msg,
		Data: data,
	})
}

func JSONError(c echo.Context, msg string) error {
	return c.JSON(http.StatusOK, &returnJSON{
		Code:	400,
		Msg:	msg,
	})
}

func JSONPagination(c echo.Context, page int, size int, total int64, list interface{}) error {
	totalPage := int(math.Ceil(float64(total) / float64(size)))
	pagination := map[string]interface{}{
		"page":  page,
		"page_size":  size,
		"total": total,
		"total_page": totalPage,
	}

	// 返回 JSON 响应
	data := map[string]interface{}{
		"pagination": pagination,
		"data": list,
	}
	
	return c.JSON(http.StatusOK, &returnJSON{
		Code:    200,
		Msg:     "success",
		Data:    data,
	})
}
